package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferProcessHistoryDaoImp;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.dto.TransferAcceptedInfoDto;
import com.tianqu.tms.dto.TransferProcessHistoryDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

public class TransferRgSearchResultAction extends TmsAction {
	
	private Logger logger = Logger.getLogger(TransferRgSearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		TransferRgActionForm inForm = (TransferRgActionForm) form;
		//
		String serial = inForm.getSerial();
		String licensePlate = inForm.getLicensePlate();
		
		if (LicensePlateConfigConstant.defLicensePlate.equals(licensePlate))
			licensePlate = "";

		String vin = inForm.getVin();
		if (StringUtil.isNullOrBlank(serial) && StringUtil.isNullOrBlank(licensePlate) && StringUtil.isNullOrBlank(vin)) {
			setErrorsToRequest(request, TmsConstants.U0039, null);// 查询的结果不存在。
			return mapping.getInputForward();
		} else if (StringUtil.isNotBlank(serial) && StringUtil.isNotBlank(licensePlate) && StringUtil.isNotBlank(vin)) {
			setErrorsToRequest(request, TmsConstants.U0039, null);// 查询的结果不存在。
			return mapping.getInputForward();
		}

		if (StringUtil.isNotBlank(serial)) {
			List<TransferAcceptedInfoDto> list = TransferAcceptedInfo.getInstance().findReCar(serial, 1);
			if (list.size() == 0) {
				setErrorsToRequest(request, TmsConstants.U0026, null);
				return mapping.getInputForward();
			}
			if (list.size() > 1) {
				setErrorsToRequest(request, TmsConstants.U1405, null);
				return mapping.getInputForward();
			}

			TransferAcceptedInfoDto dto = list.get(0);

//			if (dto.getGh_printTime() != null) {
//				setErrorsToRequest(request, TmsConstants.U1056, null);
//				return mapping.getInputForward();
//			}
			if (dto.getWaitForStatus() == 14) {
				setErrorsToRequest(request, TmsConstants.U1057, null);
				return mapping.getInputForward();
			}
			if (dto.getWaitForStatus() == 17) {
				setErrorsToRequest(request, TmsConstants.U1058, null);
				return mapping.getInputForward();
			}
			if (dto.getTransferPrice() != 1) {
				setErrorsToRequest(request, TmsConstants.U1059, null);
				return mapping.getInputForward();
			}
		} else if (StringUtil.isNotBlank(licensePlate)) {
			List<TransferAcceptedInfoDto> list = TransferAcceptedInfo.getInstance().findReCar(licensePlate, 3);
			if (list.size() == 0) {
				setErrorsToRequest(request, TmsConstants.U0039, null);
				return mapping.getInputForward();
			}
			if (list.size() > 1) {
				setErrorsToRequest(request, TmsConstants.U1405, null);
				return mapping.getInputForward();
			}

			TransferAcceptedInfoDto dto = list.get(0);
//			if (dto.getGh_printTime() != null) {
//				setErrorsToRequest(request, TmsConstants.U1056, null);
//				return mapping.getInputForward();
//			}
			if (dto.getWaitForStatus() == 14) {
				setErrorsToRequest(request, TmsConstants.U1057, null);
				return mapping.getInputForward();
			}
			if (dto.getWaitForStatus() == 17) {
				setErrorsToRequest(request, TmsConstants.U1058, null);
				return mapping.getInputForward();
			}
			if (dto.getTransferPrice() != 1) {
				setErrorsToRequest(request, TmsConstants.U1059, null);
				return mapping.getInputForward();
			}
		} else if (StringUtil.isNotBlank(vin)) {
			List<TransferAcceptedInfoDto> list = TransferAcceptedInfo.getInstance().findReCar(vin, 2);
			if (list.size() == 0) {
				setErrorsToRequest(request, TmsConstants.U0026, null);
				return mapping.getInputForward();
			}
			if (list.size() > 1) {
				setErrorsToRequest(request, TmsConstants.U1405, null);
				return mapping.getInputForward();
			}
			TransferAcceptedInfoDto dto = list.get(0);
			if (StringUtil.isNullOrBlank(dto.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0039, null);
				return mapping.getInputForward();
			}
			if (dto.getGh_printTime() != null) {
				setErrorsToRequest(request, TmsConstants.U1056, null);
				return mapping.getInputForward();
			}
			if (dto.getWaitForStatus() == 14) {
				setErrorsToRequest(request, TmsConstants.U1057, null);
				return mapping.getInputForward();
			}
			if (dto.getWaitForStatus() == 17) {
				setErrorsToRequest(request, TmsConstants.U1058, null);
				return mapping.getInputForward();
			}
			if (dto.getTransferPrice() != 1) {
				setErrorsToRequest(request, TmsConstants.U1059, null);
				return mapping.getInputForward();
			}
		}

		TransferAcceptedInfo transferAcceptedInfo = TransferAcceptedInfo.getInstance().getAcceptInfoBySerial(serial, licensePlate, vin);
		TransferRgInputViewHelper view = convert(transferAcceptedInfo);
		view.setSpecialProcess(inForm.getSpecialProcess());
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		request.setAttribute(TmsConstants.VIEWHELP, view);
 
		return mapping.findForward(TmsConstants.SUCCESS);
	}

	private TransferRgInputViewHelper convert(TransferAcceptedInfo transferAcceptedInfo) throws TmsDaoException {
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
		
		if(transferAcceptedInfo.getOwnerType() == 1) view.setIdentification(transferAcceptedInfo.getCarOwnerIdCardNo());
		else view.setIdentification(transferAcceptedInfo.getOwnerOrgNo());
		if(transferAcceptedInfo.getNewOwnerType() == 1) view.setNewIdentification(transferAcceptedInfo.getNewcarOwnerIdCardNo());
		else  view.setNewIdentification(transferAcceptedInfo.getNewOwnerOrgNo());
		
		view.setNewCarOwner(transferAcceptedInfo.getNewcarOwner());
		view.setSerial(transferAcceptedInfo.getSerial());
		view.setTransfer(transferAcceptedInfo.getTransfer());
		view.setCarMarketIn(transferAcceptedInfo.getCarMarketIn());
		view.setMaterialImages1(transferAcceptedInfo.getMaterialImages1());
		view.setMaterialImages2(transferAcceptedInfo.getMaterialImages2());
		view.setMaterialImages3(transferAcceptedInfo.getMaterialImages3());
		view.setMaterialImages4(transferAcceptedInfo.getMaterialImages4());
		view.setMaterialImages5(transferAcceptedInfo.getMaterialImages5());
		view.setMaterialImages6(transferAcceptedInfo.getMaterialImages6());
		view.setMaterialImages7(transferAcceptedInfo.getMaterialImages7());
		view.setMaterialImages8(transferAcceptedInfo.getMaterialImages8());
		view.setMaterialImages9(transferAcceptedInfo.getMaterialImages9());
		view.setMaterialImages10(transferAcceptedInfo.getMaterialImages10());
		view.setNewAgentAddress(transferAcceptedInfo.getNewAgentAddress());
		view.setNewAgentPhone(transferAcceptedInfo.getNewAgentPhone());
		view.setNewAgentPhone(transferAcceptedInfo.getNewAgentName());
		view.setNewOwnerAddress(transferAcceptedInfo.getNewOwnerAddress());
		view.setNewOwnerPhone(transferAcceptedInfo.getNewOwnerPhone());
		view.setNewOwnerOrgNo(transferAcceptedInfo.getNewOwnerOrgNo());
		view.setNewOwnerType(transferAcceptedInfo.getNewOwnerType()+"");
		view.setCy_printTime(transferAcceptedInfo.getCy_printTime());
		view.setAgentAddress(transferAcceptedInfo.getAgentAddress());
		view.setAgentPhone(transferAcceptedInfo.getAgentPhone());
		view.setAgentName(transferAcceptedInfo.getAgentName());
		view.setAgentOwnerIdCardNo(transferAcceptedInfo.getAgentOwnerIdCardNo());
		view.setTransferRegister(transferAcceptedInfo.getTransferRegister());
		view.setOwnerAddress(transferAcceptedInfo.getOwnerAddress());
		view.setOwnerPhone(transferAcceptedInfo.getOwnerPhone());
		view.setCarOwner(transferAcceptedInfo.getCarOwner());
		view.setSerial(transferAcceptedInfo.getSerial());
		view.setLicensePlate(transferAcceptedInfo.getLicensePlate());
		view.setTransfer(transferAcceptedInfo.getTransfer());
		view.setVin(transferAcceptedInfo.getVin());
		view.setCarType(transferAcceptedInfo.getCy_carType());
		view.setCy_cardType(transferAcceptedInfo.getCy_cardType());
		view.setRegisterCardNo(transferAcceptedInfo.getRegisterCardNo());
		view.setRegDate(DateUtil.DateToString11(transferAcceptedInfo.getRegDate()));
		view.setZipCode(transferAcceptedInfo.getZipCode());
		view.setPrice(StringUtil.addDH(transferAcceptedInfo.getPrice()+""));
		view.setVendorCode(transferAcceptedInfo.getVendorCode());
		view.setModelCode(transferAcceptedInfo.getModelCode());
		view.setOwnerType(transferAcceptedInfo.getOwnerType()+"");
		view.setLicensePlate(transferAcceptedInfo.getLicensePlate());
		view.setVin(transferAcceptedInfo.getVin());
		view.setAgentName(transferAcceptedInfo.getAgentName());
		view.setAgentOwnerIdCardNo(transferAcceptedInfo.getAgentOwnerIdCardNo());
		view.setAgentAddress(transferAcceptedInfo.getAgentAddress());
		view.setAgentPhone(transferAcceptedInfo.getAgentPhone());
		view.setOwnersGroupPhoto(transferAcceptedInfo.getOwnersGroupPhoto());
		if (transferAcceptedInfo.getNewOwnerType() > 0) view.setNewOwnerType(String.valueOf(transferAcceptedInfo.getNewOwnerType()));
		view.setNewOwnerAddress(transferAcceptedInfo.getNewOwnerAddress());
		view.setNewAgentPhone(transferAcceptedInfo.getNewAgentPhone());
		view.setNewAgentName(transferAcceptedInfo.getNewAgentName());
		view.setNewAgentOwnerIdCardNo(transferAcceptedInfo.getNewAgentOwnerIdCardNo());
		view.setNewAgentAddress(transferAcceptedInfo.getNewAgentAddress());
		view.setMoveOutProvince(transferAcceptedInfo.getMoveOutProvince());
		view.setMoveOutCity(transferAcceptedInfo.getMoveOutCity());
		if (transferAcceptedInfo.getTransferType() > 0)  view.setTransferType(String.valueOf(transferAcceptedInfo.getTransferType()));
		view.setNewOwnerTelPhone(transferAcceptedInfo.getNewOwnerTelPhone());
		if (transferAcceptedInfo.getOwnerType() == 1) view.setPriceBsFlag("");
		if (transferAcceptedInfo.getPrice() > 0) view.setPrice(StringUtil.addDH(String.valueOf(transferAcceptedInfo.getPrice())));
		else view.setPrice("");
		view.setTaxNo(transferAcceptedInfo.getTaxNo());
		view.setRemark(transferAcceptedInfo.getRemark());
		view.setZipCode(transferAcceptedInfo.getNewOwnerZipCode());
		view.setCarMarketIn(transferAcceptedInfo.getCarMarketIn());
		
		Codes code = Codes.getInstance();
		view.setChangeBody(transferAcceptedInfo.getChangeBody()+"");
		if(transferAcceptedInfo.getChangeBody() >0)  view.setChangeBodyName(code.getItemValueByItemIdAndItemCode(Codes.CHANGE_BODY_TYPE,transferAcceptedInfo.getChangeBody()));
		
		if(StringUtil.isNotBlank(transferAcceptedInfo.getCountryStyle())) {
			view.setCountryStyle(transferAcceptedInfo.getCountryStyle());
			view.setCountryStyleName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY,Integer.parseInt(transferAcceptedInfo.getCountryStyle())));
		}else {
			view.setCountryStyle("0");
			view.setCountryStyleName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY,0));
		}
		
		if(StringUtil.isNotBlank(transferAcceptedInfo.getCarUse())) {
			view.setCarUse(transferAcceptedInfo.getCarUse());
			view.setCarUseName(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,Integer.parseInt(transferAcceptedInfo.getCarUse())));
		}else {
			view.setCarUse("1");
			view.setCarUseName(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,1));
		}
		
		view.setCardType(transferAcceptedInfo.getCardType()+"");
		view.setCardTypeName(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,transferAcceptedInfo.getCardType()));
		
		if(transferAcceptedInfo.getGainType() > 0) {
			view.setGainType(String.valueOf(transferAcceptedInfo.getGainType()));
			view.setGainTypeName(code.getItemValueByItemIdAndItemCode(Codes.GAIN_TYPE,transferAcceptedInfo.getGainType()));
		}else {
			view.setGainType("17");
			view.setGainTypeName(code.getItemValueByItemIdAndItemCode(Codes.GAIN_TYPE,17));
		}
		
		if(StringUtil.isNotBlank(transferAcceptedInfo.getCarType()) && StringUtils.isNumeric(transferAcceptedInfo.getCarType())) {
			view.setCarType(transferAcceptedInfo.getCarType());
			view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,Integer.parseInt(transferAcceptedInfo.getCarType())));
		}else {
			view.setCarType("1");
			view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,1));
		}
				
		if(StringUtils.isNotBlank(transferAcceptedInfo.getCarMarketIn())) {
			MarketCarInfoDto info = Car.getInstance().getCtiyInfo(transferAcceptedInfo.getCarMarketIn());
			if(info!=null) view.setMarketName(info.getVaoName());
		}
		
		if(transferAcceptedInfo.getTransfer() == 2 || transferAcceptedInfo.getTransferType() ==5) view.setIsEditCarownerInfoEdit(1);//原车主可编辑
		else view.setIsEditCarownerInfoEdit(2);
		view.setStatus(transferAcceptedInfo.getStatus()+"");
		
		if(transferAcceptedInfo.getStatus() == 10 || transferAcceptedInfo.getStatus() == 11 || transferAcceptedInfo.getStatus() == 12) {
			view.setIsHidenLoadOwnerButton(1);
		}
		view.setTransferTypeValue(code.getItemValueByItemIdAndItemCode(Codes.TRANSFER_TYPE, transferAcceptedInfo.getTransferType()));
		Map<String, Integer> map = TransferAcceptedInfo.getInstance().getTransferAcceptedInfo(transferAcceptedInfo.getSerial());
		StringBuffer info = new StringBuffer("");
		int transferRegister = transferAcceptedInfo.getTransferRegister();
		if(transferRegister == 3 || transferRegister == 4 || transferRegister == 5) {
			if(map.containsKey("epStandard") && map.get("epStandard") == 2) info.append(" 环保达标:未达标 ");
			if(map.containsKey("carReplaceType") &&map.get("carReplaceType") == 2) info.append(" 客车换型:换 ");
			if(map.containsKey("carPeccancy") &&map.get("carPeccancy") == 2) info.append(" 车辆违章:违章 ");
			if(map.containsKey("overdue") &&map.get("overdue") == 2) info.append(" 超检审核:车辆检验逾期 ");
			if(map.containsKey("mortgage") &&map.get("mortgage") == 2) info.append(" 抵押审核:已抵押 ");
			if(map.containsKey("sealup") &&map.get("sealup") == 2) info.append(" 查封审核:查封 ");
			if(map.containsKey("registration") &&map.get("registration") == 2) info.append(" 登记证书:无 ");
			if(map.containsKey("drivingLicense") &&map.get("drivingLicense") == 2) info.append(" 行驶证:不符 ");
			if(map.containsKey("scrapOverdue") &&map.get("scrapOverdue") == 2) info.append(" 报废超期:超期 ");
			if(map.containsKey("robbery") &&map.get("robbery") == 2) info.append(" 盗抢审核:盗抢 ");
			if(map.containsKey("ownerEscaping") &&map.get("ownerEscaping") == 2) info.append(" 原车主在逃:在逃 ");
			if(map.containsKey("ownerScrap") &&map.get("ownerScrap") == 2) info.append(" 原车主报废车:强制报废 ");
			if(map.containsKey("ownerLive") &&map.get("ownerLive") == 2) info.append(" 原车主居住证:无居住证明 ");
			if(map.containsKey("newEscaping") &&map.get("newEscaping") == 2) info.append(" 新车主在逃:在逃 ");
			if(map.containsKey("newScrap") &&map.get("newScrap") == 2) info.append(" 新车主报废车:强制报废 ");
			if(map.containsKey("newLive") &&map.get("newLive") == 2) info.append(" 新车主居住证:无居住证明 ");
		}
		view.setAuditInfo(info.toString());
		view.setReason("");
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			TransferProcessHistoryDaoImp dao = (TransferProcessHistoryDaoImp) TransferProcessHistoryDaoImp.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
			List<TransferProcessHistoryDto> list = dao.loadTransferProcessHistoryBySerial(transferAcceptedInfo.getSerial());
			for (TransferProcessHistoryDto dto : list) {
				if(dto.getBusinessType() == 47) {
					view.setReason(dto.getReason());
					break;
				}
			}
			controller.commit();
		} catch (Exception e) {
			logger.error("get reason error. serial:" + transferAcceptedInfo.getSerial());
			logger.error("get reason error. error info :" + e.toString());
		}
		return view;
	}

	 
}
